home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
5
/
DINKDEMO
/
DINKCLAS
/
DSCROLLW.C
< prev
next >
Wrap
Text File
|
1992-07-23
|
11KB
|
554 lines
/*
File: DScrollWindow.c
Written by: Mark Gross
Copyright: ⌐ 1992 by Applied Technical Software, all rights reserved.
Use at your own risk.
*/
// This is the implemntation of DScrollWindow class (aka DScrollWindow)
#include <DApplication.h>
#include "DScrollWindow.h"
DScrollWindow::DScrollWindow(void)
{
//null
}
DScrollWindow::~DScrollWindow(void)
{
//null
}
Boolean DScrollWindow::Init(DDocument *doc, Boolean hasColorWindows)
{
Boolean inheritedSuccess;
Rect r;
SetRect(&r, 0, 0, 0, 0);
inheritedSuccess = inherited::Init(doc, hasColorWindows);
fHorizScrollBar = NULL;
fVertScrollBar = NULL;
fVMin = 0;
fVMax = 444;
fHMin = 0;
fHMax = 444;
if (inheritedSuccess)
{
SetPort(fWindowPtr);
fHorizScrollBar = NewControl(fWindowPtr,&r, "",TRUE,
0, 0, 0, scrollBarProc, 0);
fVertScrollBar = NewControl(fWindowPtr,&r, "",TRUE,
0, 0, 0, scrollBarProc, 0);
SizeScrollBars();
SynchScrollBars();
InvalRect(&(fWindowPtr->portRect));
}
return ( (fHorizScrollBar != NULL)&&(fVertScrollBar != NULL) && inheritedSuccess);
}// end of init function
void DScrollWindow::KillMeNext(void)
{
KillControls(fWindowPtr);
inherited::KillMeNext();
}
void DScrollWindow::HandleUpdateEvt(EventRecord *theEvent)
{
Rect r;
SetPort (fWindowPtr);
BeginUpdate(fWindowPtr);
FocusOnContent();
r = (**(fWindowPtr->visRgn)).rgnBBox;
if( !EmptyRgn(fWindowPtr->visRgn) )
Draw(&r);
FocusOnWindow();
DrawControls(fWindowPtr);
DrawGrowIcon( fWindowPtr);
EndUpdate(fWindowPtr);
}// end of HandleUpdateEvt member function
void DScrollWindow::HandleActivateEvt(EventRecord *theEvent)
{
Boolean activating;
activating = theEvent->modifiers & activeFlag;
if(activating)
{
FocusOnWindow();
ShowControl(fVertScrollBar);
ShowControl(fHorizScrollBar);
DrawControls(fWindowPtr);
DrawGrowIcon(fWindowPtr);
FocusOnContent();
if( !EmptyRgn(fWindowPtr->visRgn) )
Draw( &(fWindowPtr->portRect));
}
else
{
FocusOnWindow();
HideControl(fVertScrollBar);
HideControl(fHorizScrollBar);
}
}// end of HandleActivateEvt member function
void DScrollWindow::HandleOSEvent(EventRecord *theEvent)
{
if (gApplication->fInBackground)
{
FocusOnWindow();
HideControl(fVertScrollBar);
HideControl(fHorizScrollBar);
}
else
{
FocusOnWindow();
ShowControl(fVertScrollBar);
ShowControl(fHorizScrollBar);
DrawControls(fWindowPtr);
DrawGrowIcon(fWindowPtr);
FocusOnContent();
}
inherited::HandleOSEvent(theEvent);
}// end of HandleOSEvent member function
short DScrollWindow::GetVertLineScrollAmount(void)
{
return 16;
}//end of GetVertLineScrollAmount function
short DScrollWindow::GetHorizLineScrollAmount(void)
{
return 16;
}//end of GetHorizLineScrollAmount function
short DScrollWindow::GetVertPageScrollAmount(void)
{
Rect r;
GetContentRect(&r);
return r.bottom - r.top - kScrollOverlap;
}//end of GetVertPageScrollAmount function
short DScrollWindow::GetHorizPageScrollAmount(void)
{
Rect r;
GetContentRect(&r);
return r.right - r.left - kScrollOverlap;
}//end of GetHorizPageScrollAmount function
void DScrollWindow::DoGrow(EventRecord *theEvent)
{
FocusOnWindow();
inherited::DoGrow(theEvent);
SizeScrollBars();
SynchScrollBars();
}//end of DoGrow function
void DScrollWindow::DoZoom(short partCode)
{
FocusOnWindow();
inherited::DoZoom(partCode);
SizeScrollBars();
SynchScrollBars();
}//end of DoZoom function
void DScrollWindow::DoContent(EventRecord *theEvent)
{
Rect contents;
FocusOnWindow();
GlobalToLocal(&theEvent->where);
GetContentRect(&contents);
if(PtInRect(theEvent->where, &contents))
{
FocusOnContent();
inherited::DoContent(theEvent);
}
else
ScrollClick(theEvent);
}// end of DoContent member function
void DScrollWindow::SizeScrollBars()
{
Rect r, r_temp;
FocusOnWindow();
r = fWindowPtr->portRect;
HideControl(fVertScrollBar);
SizeControl(fVertScrollBar, kScrollBarWidth,
(r.bottom - r.top - kScrollBarPos) + 2);
MoveControl(fVertScrollBar, r.right - kScrollBarPos, -1);
ShowControl(fVertScrollBar);
r_temp = (**fVertScrollBar).contrlRect;
ValidRect(&r_temp);
InvalRect(&r_temp);
HideControl(fHorizScrollBar);
SizeControl(fHorizScrollBar, (r.right - r.left - kScrollBarPos) + 2,
kScrollBarWidth);
MoveControl(fHorizScrollBar, -1, r.bottom - r.top - kScrollBarPos);
ShowControl(fHorizScrollBar);
r_temp = (**fHorizScrollBar).contrlRect;
ValidRect(&r_temp);
InvalRect(&r_temp);
}// end of SizeScroll Bars member fuction
void DScrollWindow::ValidateScrollRange(void)
{
Rect r;
short dh, dv;
short currentValue;
short newMax;
RgnHandle oldClip;
GetContentRect(&r);
dh = 0; dv = 0;
newMax = (fVMax - fVMin) - (r.bottom - r.top);
// see .h file for comment on control ranges vrs visible ranges
if(newMax < 0)
newMax = 0;
if(fVOffSet > newMax)
dv = fVOffSet - newMax;
SetCtlMax(fVertScrollBar, newMax);
newMax = (fHMax - fHMin) - (r.right - r.left);
if(newMax < 0)
newMax = 0;
if(fHOffSet > newMax)
dh = fHOffSet - newMax;
SetCtlMax(fHorizScrollBar, newMax);
if(dh | dv)
{
FocusOnContent();
GetContentRect(&r);
InvalRect(&r);
oldClip = NewRgn();
GetClip(oldClip);
SetRect(&r, 0, 0, 0, 0);
ClipRect(&r);
ScrollContents(dh,dv);
SetClip(oldClip);
DisposeRgn(oldClip);
}
}// end of ValidateScrollRange member function
void DScrollWindow::ValidateVertScrollRange(void)
{
Rect r;
short dv;
short currentValue;
short newMax;
RgnHandle oldClip;
GetContentRect(&r);
dv = 0;
newMax = (fVMax - fVMin) - (r.bottom - r.top);
if(newMax < 0)
newMax = 0;
if(fVOffSet > newMax)
dv = fVOffSet - newMax;
SetCtlMax(fVertScrollBar, newMax);
if(dv)
{
FocusOnContent();
GetContentRect(&r);
InvalRect(&r);
oldClip = NewRgn();
GetClip(oldClip);
SetRect(&r, 0, 0, 0, 0);
ClipRect(&r);
ScrollContents(0,dv);
SetClip(oldClip);
DisposeRgn(oldClip);
}
}// end of ValidateVertScrollRange member function
void DScrollWindow::SynchScrollBars(void)
{
ValidateScrollRange();
FocusOnWindow();
SetCtlValue(fHorizScrollBar, fHOffSet);
SetCtlValue(fVertScrollBar, fVOffSet);
}// end of SynchScrollBars member function
void DScrollWindow::ScrollClick(EventRecord *theEvent)
{
ControlHandle whichControl;
short part;
FocusOnWindow();
if(part = FindControl(theEvent->where, fWindowPtr, &whichControl) )
{
switch(part)
{
case inThumb:
DoThumbScroll(whichControl, theEvent->where);
break;
case inUpButton:
case inDownButton:
DoButtonScroll(whichControl, theEvent->where);
break;
case inPageUp:
case inPageDown:
DoPageScroll(whichControl,part);
break;
}/*end of switch*/
}
}// end of ScrollClick member function
void DScrollWindow::DoThumbScroll(ControlHandle theControl, Point localPt)
{
short oldValue, trackResult, newValue, diff;
oldValue = GetCtlValue(theControl);
trackResult = TrackControl(theControl, localPt, NULL);
if(trackResult != 0)
{
newValue = GetCtlValue(theControl);
diff = newValue - oldValue;
if(theControl == fHorizScrollBar)
DoHScroll(diff);
if(theControl == fVertScrollBar)
DoVScroll(diff);
}
}// end of DoThumbScroll member function
void DScrollWindow::DoPageScroll(ControlHandle theControl, short part)
{
short scrollAmount, currentPart;
Point thePt;
if(theControl == fVertScrollBar)
{
scrollAmount = GetVertPageScrollAmount();
do
{
GetMouse(&thePt);
currentPart = TestControl(theControl, thePt);
if(currentPart == part)
{
if(currentPart == inPageUp)
DoVScroll(- scrollAmount);
if(currentPart == inPageDown)
DoVScroll(scrollAmount);
}
}while(Button());
}// end if VeriticalScrollBar
else if (theControl == fHorizScrollBar)
{
scrollAmount = GetHorizPageScrollAmount();
do
{
GetMouse(&thePt);
currentPart = TestControl(theControl, thePt);
if(currentPart == part)
{
if(currentPart == inPageUp)
DoHScroll(- scrollAmount);
if(currentPart == inPageDown)
DoHScroll(scrollAmount);
}
}while(Button());
}// end ifHorizontal Scroll Bar
}// end of DoPageScroll function
void DScrollWindow::DoButtonScroll(ControlHandle theControl, Point localPt)
{
short result;
result = TrackControl(theControl, localPt, (ProcPtr) ActionProc) ;
}// end of DoButtonScroll member function
void DScrollWindow::ScrollContents(short dh, short dv)
{
Rect r;
RgnHandle updateRgn;
GetContentRect(&r);
updateRgn = NewRgn();
ScrollRect(&r,dh,dv,updateRgn);
fVOffSet -= dv;
fHOffSet -= dh;
InvalRgn(updateRgn);
HandleUpdateEvt(NULL);
DisposeRgn(updateRgn);
}// end of ScrollContents function
void DScrollWindow::DoHScroll( short change)
{
RgnHandle oldClip;
short diff;
short newValue;
short oldMin, oldMax, oldCurrent;
oldClip = NewRgn();
GetClip(oldClip);
diff = 0;
newValue = fHOffSet + change;
if(change<0)
{
oldMin = GetCtlMin(fHorizScrollBar);
if(newValue < oldMin)
newValue = oldMin;
}
else
{
oldMax = GetCtlMax(fHorizScrollBar);
if(newValue > oldMax)
newValue = oldMax;
}
diff = fHOffSet - newValue;
FocusOnContent();
ScrollContents(diff,0);
FocusOnWindow();
SetCtlValue(fHorizScrollBar, fHOffSet);
SetClip(oldClip);
DisposeRgn(oldClip);
}// end of DoHScroll member function
void DScrollWindow::DoVScroll(short change)
{
RgnHandle oldClip;
short diff;
short newValue;
short oldMin, oldMax, oldCurrent;
oldClip = NewRgn();
GetClip(oldClip);
diff = 0;
newValue = fVOffSet + change;
if(change<0)
{
oldMin = GetCtlMin(fVertScrollBar);
if(newValue < oldMin)
newValue = oldMin;
}
else
{
oldMax = GetCtlMax(fVertScrollBar);
if(newValue > oldMax)
newValue = oldMax;
}
diff = fVOffSet - newValue;
FocusOnContent();
ScrollContents(0, diff);
FocusOnWindow();
SetCtlValue(fVertScrollBar, fVOffSet);
SetClip(oldClip);
DisposeRgn(oldClip);
}// DoVScroll member function
pascal void DScrollWindow::ActionProc(ControlHandle theControl, short partCode)
{
short scrollAmount = 0;
DScrollWindow *theCurrScrollWindow;
theCurrScrollWindow = (DScrollWindow *) GetWRefCon( FrontWindow() );// isn't system7 great?
if(theControl == theCurrScrollWindow->fVertScrollBar )
{
scrollAmount = theCurrScrollWindow->GetVertLineScrollAmount();
if(partCode == inUpButton)
theCurrScrollWindow->DoVScroll(-scrollAmount);
if(partCode == inDownButton)
theCurrScrollWindow->DoVScroll(scrollAmount);
}
else if( theControl == theCurrScrollWindow->fHorizScrollBar)
{
scrollAmount = theCurrScrollWindow->GetHorizLineScrollAmount();
if(partCode == inUpButton)
theCurrScrollWindow->DoHScroll(-scrollAmount);
if(partCode == inDownButton)
theCurrScrollWindow->DoHScroll(scrollAmount);
}
}// end of ActionProc proc